package com.jiugang.cardump.controller;


import com.alibaba.fastjson.JSONObject;
import com.jiugang.cardump.bean.InformationBean;
import com.jiugang.cardump.constant.StatusCodeConstant;
import com.jiugang.cardump.entity.Pro;
import com.jiugang.cardump.exception.SystemExceptionV110;
import com.jiugang.cardump.pojo.CarDumperPOJO;
import com.jiugang.cardump.pojo.ProEquPOJO;
import com.jiugang.cardump.result.ResultInfoV110;
import com.jiugang.cardump.service.ProService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-08-19
 */
@RestController
@RequestMapping("/pro")
@Slf4j
public class ProController {

    @Autowired
    ProService proService;

    /**
     * @Author luoxin
     * @Description 添加项目接口
     * @Date 3:31 下午 2021/8/20
     * @param proEquPOJOStr
     * @return
     */
    @PostMapping
    public ResultInfoV110 add (String proEquPOJOStr){
        log.info("MonitorController的add方法进入，参数为" + proEquPOJOStr);

        //校验参数
        if (proEquPOJOStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "项目数据"));
        }
        //字符串转换Json对象

        JSONObject jsonObject = JSONObject.parseObject(proEquPOJOStr);

        //json对象转换baomingpojo对象
        ProEquPOJO proEquPOJO = JSONObject.toJavaObject(jsonObject, ProEquPOJO.class);

        String[] splits = proEquPOJO.getEquIdsStr().split(",");

        Integer[] ids = new Integer[splits.length];

        for (int i = 0; i < splits.length;i++) {
            ids[i] = Integer.parseInt(splits[i]);
        }

       proEquPOJO.setEquIds(ids);

       return proService.insert(proEquPOJO);
    }

    /**
     * @Author luoxin
     * @Description 修改项目接口
     * @Date 3:32 下午 2021/8/20
     * @param proEquPOJOStr
     * @return
     */
    @PutMapping
    public ResultInfoV110 update (String proEquPOJOStr){
        log.debug("MonitorController的update方法进入，参数为" + proEquPOJOStr);

        //校验参数
        if (proEquPOJOStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "项目数据"));
        }

        //字符串转换Json对象
        JSONObject jsonObject = JSONObject.parseObject(proEquPOJOStr);
        //json对象转换baomingpojo对象
        ProEquPOJO proEquPOJO = JSONObject.toJavaObject(jsonObject, ProEquPOJO.class);
        String[] splits = proEquPOJO.getEquIdsStr().split(",");

        Integer[] ids = new Integer[splits.length];

        for (int i = 0; i < splits.length;i++) {
            ids[i] = Integer.parseInt(splits[i]);
        }

        proEquPOJO.setEquIds(ids);

        return proService.update(proEquPOJO);
    }

    /**
     * @Author luoxin
     * @Description 项目启动，关闭接口
     * @Date 3:32 下午 2021/8/20
     * @param id，statu
     * @return
     */
    @PutMapping("/statu")
    public ResultInfoV110 update (Integer id,Integer statu){
        log.debug("MonitorController的update方法进入，参数为" + id);

        //校验参数
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "传入数据"));
        }

        return proService.updateStatus(id,statu);
    }

    /**
     * @Author luoxin
     * @Description 查询项目列表接口
     * @Date 3:33 下午 2021/8/20
     * @param
     * @return
     */
    @GetMapping("/list")
    public ResultInfoV110 selectList (Pro pro){
        log.debug("MonitorController的selectList方法进入，参数为"+pro);
        return proService.select(pro);
    }

    /**
     * @Author luoxin
     * @Description 删除项目接口
     * @Date 3:25 下午 2021/8/21
     * @param
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public ResultInfoV110 remove(@PathVariable("id") Integer proId){
        log.debug("MonitorController的remove方法进入，参数为" +proId);
        return proService.delete(proId);
    }

    /**
     * @Author luoxin
     * @Description 根据Id查询项目信息
     * @Date 5:31 下午 2021/8/21
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public ResultInfoV110 selectById (@PathVariable("id") Integer proId){
        log.debug("MonitorController的selectById方法进入，参数为"+ proId );
        return proService.selectById(proId);
    }
}

